﻿namespace RuoVea.OmiApi.SystemApp.Service;

/// <summary>
/// 系统用户扩展机构服务
/// </summary>
public class SysUserExtOrgService
{
    private readonly SugarRepository<SysUserExtOrg> _sysUserExtOrgRep;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysUserExtOrgRep"></param>
    public SysUserExtOrgService(SugarRepository<SysUserExtOrg> sysUserExtOrgRep)
    {
        _sysUserExtOrgRep = sysUserExtOrgRep;
    }

    /// <summary>
    /// 获取用户扩展机构集合
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task<List<SysUserExtOrg>> GetUserExtOrgList(long userId)
    {
        return await _sysUserExtOrgRep.GetListAsync(u => u.UserId == userId && u.IsDefault == YesOrNot.N);
    }

    /// <summary>
    /// 更新用户扩展机构
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="extOrgList"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task UpdateUserExtOrg(long userId, List<SysUserExtOrg> extOrgList, AddUserInput input)
    {
        if(input.OrgId<=0) 
            throw new ArgumentException(i18n.org_select);

        if (input.PosId <= 0)
            throw new ArgumentException(i18n.position_select);

        await _sysUserExtOrgRep.DeleteAsync(u => u.UserId == userId);

        var mainOrg = new SysUserExtOrg
        {
            UserId = input.Id,
            PosId = input.PosId,
            IsDefault = YesOrNot.Y,
            OrgId = input.OrgId,
            JoinDate = input.JoinDate,
            JobNum = input.JobNum
        };

        if (extOrgList == null || extOrgList.Count == 0)
            extOrgList = new List<SysUserExtOrg> { mainOrg };

        // 处理传入的扩展机构列表
        bool hasMainOrg = false;

        foreach (var extOrg in extOrgList)
        {
            extOrg.UserId = userId;
            if (extOrg.OrgId == input.OrgId)
            {
                extOrg.PosId = input.PosId;
                extOrg.JoinDate = input.JoinDate;
                extOrg.JobNum = input.JobNum;
                extOrg.IsDefault = YesOrNot.Y;
                hasMainOrg = true;
            }
            else
            {
                extOrg.IsDefault = YesOrNot.N;
            }
        }

        // 如果列表中不包含主机构，添加主机构
        if (!hasMainOrg)
            extOrgList.Add(mainOrg);

        await _sysUserExtOrgRep.InsertRangeAsync(extOrgList);
    }

    /// <summary>
    /// 根据机构Id集合删除扩展机构
    /// </summary>
    /// <param name="orgIdList"></param>
    /// <returns></returns>
    public async Task DeleteUserExtOrgByOrgIdList(List<long> orgIdList)
    {
        await _sysUserExtOrgRep.DeleteAsync(u => orgIdList.Contains(u.OrgId));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="orgId"></param>
    /// <param name="orgPid"></param>
    /// <returns></returns>
    public List<long> GetUserIdListByOrg(long orgId, long orgPid)
    {
        return _sysUserExtOrgRep.AsQueryable().Where(x => x.OrgId == orgId || x.OrgId == orgPid).Select(x => x.UserId).ToList();
    }

    /// <summary>
    /// 根据用户Id删除扩展机构
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task DeleteUserExtOrgByUserId(long userId)
    {
        await _sysUserExtOrgRep.DeleteAsync(u => u.UserId == userId);
    }

    /// <summary>
    /// 根据机构Id判断是否有用户
    /// </summary>
    /// <param name="orgId"></param>
    /// <returns></returns>
    public async Task<bool> HasUserOrg(long orgId)
    {
        return await _sysUserExtOrgRep.IsAnyAsync(u => u.OrgId == orgId);
    }

    /// <summary>
    /// 根据职位Id判断是否有用户
    /// </summary>
    /// <param name="posId"></param>
    /// <returns></returns>
    public async Task<bool> HasUserPos(long posId)
    {
        return await _sysUserExtOrgRep.IsAnyAsync(u => u.PosId == posId);
    }
}